package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.OnlineIM;
import com.cz.zslu.entity.OriginAuthorization;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.entity.ZslStore;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 授权品牌 前端控制器
 * </p>
 *
 * @author 森森
 * @since 2018-12-17
 */
@RestController
@Api(description = "授权品牌")
@RequestMapping("/originAuthorization")
public class OriginAuthorizationController {
    @Autowired
    private IOriginAuthorizationService iOriginAuthorizationService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private IZslStoreService iZslStoreService;
    @Autowired
    private IZslOriginService iZslOriginService;
    @Autowired
    private OnlineIMService onlineIMService;
    @Autowired
    private IZslStoreService zslStoreService;

    @ApiOperation("申请品牌授权")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ReturnObject add(@ApiParam("审核内容描述") String describe,
                            @ApiParam("审核内容图片") String imgUrl,
                            @ApiParam("品牌id") Long originId) {
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        OriginAuthorization info = new OriginAuthorization();
        info.setDescribe(describe);
        info.setImgUrl(imgUrl);
        info.setOriginId(originId);
        info.setOriginName(iZslOriginService.selectById(originId).getName());
        info.setStatus(0);
        info.setUserId(BaseContextHandler.getUserID());
        info.setStoreId(sysUser.getStoreId());
        iOriginAuthorizationService.insert(info);

        return new ReturnObject<>(0, "获取成功", info);
    }

    @ApiOperation("修改品牌授权")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ReturnObject edit(@ApiParam("审核内容描述") Long id,
                             @ApiParam("审核内容描述") String describe,
                             @ApiParam("审核内容图片") String imgUrl,
                             @ApiParam("品牌id") Long originId) {
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        OriginAuthorization info = iOriginAuthorizationService.selectById(id);
        info.setDescribe(describe);
        info.setImgUrl(imgUrl);
        info.setOriginId(originId);
        info.setOriginName(iZslOriginService.selectById(originId).getName());
        info.setStatus(0);
        info.setUserId(BaseContextHandler.getUserID());
        info.setStoreId(sysUser.getStoreId());
        iOriginAuthorizationService.insert(info);
        return new ReturnObject<>(0, "获取成功", info);
    }


    @ApiOperation("获取待审核的品牌授权")
    @RequestMapping(value = "/getAuditPage", method = RequestMethod.POST)
    public ReturnObject<List<OriginAuthorization>> getAuditPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                                @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                                @ApiParam("品牌名称") @RequestParam String name,
                                                                @ApiParam("审核状态（0：待审核，1：审核成功，2：审核失败）") Integer status) {
        EntityWrapper<OriginAuthorization> ew = new EntityWrapper<>();
        if (status != null) {

            ew.eq("status", status);
        }
        if (StringUtils.isNotBlank(name)) {
            ew.like("origin_name", name);
        }
        ew.orderBy("crt_time", false);
        Page<OriginAuthorization> obj = iOriginAuthorizationService.selectPage(new Page<>(offset, limit), ew);
        for (OriginAuthorization info : obj.getRecords()) {
            info.setSysUser(iSysUserService.selectById(info.getUserId()));
            info.setZslStore(iZslStoreService.selectById(info.getStoreId()));
            info.setZslOrigin(iZslOriginService.selectById(info.getOriginId()));
        }
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取我的授权品牌")
    @RequestMapping(value = "/getMyPage", method = RequestMethod.POST)
    public ReturnObject<List<OriginAuthorization>> getMyPage(@ApiParam("审核状态（0：待审核，1：审核成功，2：审核失败）") Integer status) {
        EntityWrapper<OriginAuthorization> ew = new EntityWrapper<>();
        if (status != null) {
            ew.eq("status", status);
        }
        ew.eq("user_id", BaseContextHandler.getUserID());
        ew.orderBy("crt_time", false);
        List<OriginAuthorization> obj = iOriginAuthorizationService.selectList(ew);
        for (OriginAuthorization info : obj) {
            info.setSysUser(iSysUserService.selectById(info.getUserId()));
            info.setZslStore(iZslStoreService.selectById(info.getStoreId()));
            info.setZslOrigin(iZslOriginService.selectById(info.getOriginId()));
        }
        return new ReturnObject<>(0, "获取成功", obj);
    }


    @ApiOperation("审核品牌授权")
    @RequestMapping(value = "/audit", method = RequestMethod.POST)
    public ReturnObject audit(@ApiParam("id") Long id,
                              @ApiParam("审核状态（1：审核成功，2：审核失败）") Integer status,
                              @ApiParam("审核原因") @RequestParam String content) {
        OriginAuthorization originAuthorization = iOriginAuthorizationService.selectById(id);
        originAuthorization.setStatus(status);
        iOriginAuthorizationService.updateById(originAuthorization);
        //申请品牌审核的用户
        SysUser toUser = iSysUserService.selectById(originAuthorization.getUserId());
        //系统管理员
        SysUser fromSysUser = iSysUserService.selectById(BaseContextHandler.getUserID());

        OnlineIM onlineIM = new OnlineIM();
        onlineIM.setType(4);
        onlineIM.setName(fromSysUser.getUsername());
        onlineIM.setFromUserId(fromSysUser.getId());
        onlineIM.setHeadPic(fromSysUser.getHeadPortraits());
        onlineIM.setPhone(fromSysUser.getPhone());
        if (status == 1) {
            onlineIM.setContent("品牌审核提醒：您申请的[" + originAuthorization.getOriginName() + "]审核成功！审核原因：" + content);
        } else {
            onlineIM.setContent("品牌审核提醒：您申请的[" + originAuthorization.getOriginName() + "]审核失败,请检测后重新申请！审核原因：" + content);
        }
        onlineIM.setIsDeal(1);
        onlineIM.setStoreId(toUser.getStoreId());
        onlineIM.setUserId(originAuthorization.getUserId());
        ZslStore store = zslStoreService.selectById(toUser.getStoreId());
        onlineIM.setStoreName(store.getName());
        onlineIMService.insert(onlineIM);

        return new ReturnObject<>(0, "审核成功");
    }
}

